package com.app.controller;

import com.app.mapper.InterviewRecordMapper;
import com.app.model.ComRecruitersModel;
import com.app.model.CustomerModel;
import com.app.model.InterviewRecordModel;
import com.app.model.UserModel;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;


@Controller
@RestController
@NoArgsConstructor
@AllArgsConstructor
@Data
public class InterviewRecordController {
    @Autowired
    InterviewRecordService interviewRecordService;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    CustomerService customerService;

    @Autowired
    UserService userService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    JobPublishService jobPublishService;


    //面试记录列表
    @PostMapping("/interviewRecordList")
    public Result<Object> interviewRecordList(
            @RequestParam("userType") int userType,
            @RequestParam("mobile") String mobile,
            HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        String tokenReturn = TokenUtils.nothingness(token);
        if (tokenReturn == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(tokenReturn);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(interviewRecordService.interviewRecordList(mobile, userType, user));
    }


    //Id查询面试记录
    @GetMapping("/getInterviewRecord")
    public Result<Object> getInterviewRecord(HttpServletRequest request) {
        String token = request.getHeader("token");
        int id = Integer.valueOf(request.getParameter("id"));
        String tokenReturn = TokenUtils.nothingness(token);
        if (tokenReturn == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(tokenReturn);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(jobPublishService.getInterviewRecord(id));

    }

    //新增面试记录
    @PostMapping("/editInterviewRecord")
    public Result<Object> editInterviewRecord(
            @RequestParam(required = false) String interviewTime,
            @RequestParam("plan") int plan,
            @RequestParam(required = false) Integer pid,
            @RequestParam("id") int id,
            @RequestParam(required = false) String mobile,
            HttpServletRequest request) throws ParseException {
        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        String token = request.getHeader("token");
        String tokenReturn = TokenUtils.nothingness(token);
        if (tokenReturn == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(tokenReturn);

        InterviewRecordModel model = new InterviewRecordModel();
        ComRecruitersModel com = null;
        if(user!=null&&user.getRId()!=null&&user.getRId()>0)
        {
            com = comRecruitersService.getComRecruitersById(user.getRId());
        }
        if (id > 0) {
            model = interviewRecordService.getInterviewRecordById(id);
            model.setPlan(plan);
            model.setUpTime(CommonHelp.getNowTimestamp());
            interviewRecordService.update(model);
        } else {
            UserModel userCustomer = userService.getUserByMobile(mobile);
            CustomerModel cus = customerService.getCustomerById(userCustomer.getCId());
            model.setPlan(plan);
            model.setRid(com.getId());
            model.setCid(cus.getId());
            model.setInterviewTime(String.valueOf(interviewTime));
            model.setPid(pid);
            model.setCrtTime(CommonHelp.getNowTimestamp());
            interviewRecordService.insert(model);
            json.put("id", model.getId());
        }

        return Result.success(json);
    }
}